﻿/// <reference path="jquery-1.4.1.js" />
function HandleDropDownEvent(sender, event) {
    var controlContainer = $(sender).parents("[name=MultiSelectContainer]:first");
    var listContainer = controlContainer.find("[name=ListContainer]");
    if (listContainer.css("display") == "none") {
        $(document).find("[name=MultiSelectContainer] input:text").filter(":not(#" + controlContainer.find("input:text").attr("id") + ")").each(function () { HandleCollapseEvent(this, null); });
        listContainer.find("input:checkbox").css("display", "none");
        listContainer.slideDown("fast", null);
        controlContainer.find(".MultiSelect__EnabledControl").removeClass("MultiSelect__EnabledControl").addClass("MultiSelect__ActiveControl");
        controlContainer.find(".MultiSelect__ErrorControl").removeClass("MultiSelect__ErrorControl").addClass("MultiSelect__ActiveControl").addClass("MultiSelect__ErrorControlCssId");
        controlContainer.find(".MultiSelect__Label").removeClass("MultiSelect__Label").addClass("MultiSelect__ActiveLabel");
        controlContainer.find(".MultiSelect__ErrorLabel").removeClass("MultiSelect__ErrorLabel").addClass("MultiSelect__ActiveLabel").addClass("MultiSelect__ErrorLabelCssId"); 
        $(document).one("click", function (innerEvent) {
            HandleCollapseEvent(sender, innerEvent);
        });
        controlContainer.find("[name=ActivateButton]").unbind("click").click(function (innerEvent) {
            HandleCollapseEvent(sender, innerEvent);
        });
        controlContainer.find("INPUT:text").unbind("click").click(function (innerEvent) {
            HandleCollapseEvent(sender, innerEvent);
        });
    }
    if (event != null) {
        event.stopPropagation();
    }
}
function HandleCollapseEvent(sender, event) {
    var controlContainer = $(sender).parents("[name=MultiSelectContainer]:first");
    var listContainer = controlContainer.find("[name=ListContainer]");
    var postBackOnChangeUniqueId = controlContainer.find("INPUT:hidden[name=PostBackOnChangeUniqueId]").val();
    if (listContainer.css("display") != "none") {
        listContainer.slideUp(0, null);
        controlContainer.find(".MultiSelect__ActiveControl").removeClass("MultiSelect__ActiveControl").addClass("MultiSelect__EnabledControl");
        controlContainer.find(".MultiSelect__ErrorControlCssId").removeClass("MultiSelect__ErrorControlCssId").removeClass("MultiSelect__ActiveControl").addClass("MultiSelect__ErrorControl");
        controlContainer.find(".MultiSelect__ActiveLabel").removeClass("MultiSelect__ActiveLabel").addClass("MultiSelect__Label");
        controlContainer.find(".MultiSelect__ErrorLabelCssId").removeClass("MultiSelect__ErrorLabelCssId").removeClass("MultiSelect__ActiveLabel").addClass("MultiSelect__ErrorLabel");
        controlContainer.find("[name=ActivateButton]").unbind("click").click(function (innerEvent) {
            HandleDropDownEvent(sender, innerEvent);
        });
        controlContainer.find("INPUT:text").unbind("click").click(function (innerEvent) {
            HandleDropDownEvent(sender, innerEvent);
        });
        if (postBackOnChangeUniqueId != null && postBackOnChangeUniqueId.length > 0 && controlContainer.hasClass("MultiSelect_ChangedFieldCssId")) {
            __doPostBack(postBackOnChangeUniqueId, '');
        }
    }
    if (event != null) {
        event.stopPropagation();
    }
}
function HandleSelectItemClickEvent(sender, event, emptyMessage, doNotListSelectedInTextBox) {
    var checkBox = $(sender).find("input:checkbox").first();
    var controlContainer = $(sender).parents("[name=MultiSelectContainer]:first");
    checkBox.attr("checked", !$(checkBox).attr('checked'));
    if (checkBox.attr("checked")) {
        var checkedImage = controlContainer.find("#" + controlContainer.attr("ID") + "_ImgChecked").css("background-image");
        $(sender).find(".MultiSelect__CheckBox").css("background-image", checkedImage);
    }
    else {
        var unCheckedImage = controlContainer.find("#" + controlContainer.attr("ID") + "_ImgUnchecked").css("background-image");
        $(sender).find(".MultiSelect__CheckBox").css("background-image", unCheckedImage);
    }

    if (!doNotListSelectedInTextBox) {
        var currentText = "";
        controlContainer.find("input:checked").each(function () {
            if (currentText.length > 0)
                currentText += ", ";
            currentText += $(this).parent().find("div:last").text();
        });
        var inputItem = controlContainer.find("input:text");
        if (currentText == "") {
            inputItem.val(emptyMessage);
            inputItem.attr("title", emptyMessage);
            inputItem.addClass("MultiSelect__EmptyTextBox");
        }
        else {
            inputItem.val(currentText);
            inputItem.attr("title", currentText);
            inputItem.removeClass("MultiSelect__EmptyTextBox");
        }
    }
    controlContainer.removeClass("MultiSelect_ChangedFieldCssId");
    controlContainer.addClass("MultiSelect_ChangedFieldCssId");
    if (event != null) {
        event.stopPropagation();
    }
}
function HandleClearEvent(sender, event, emptyMessage, doNotListSelectedInTextBox) {
    var controlContainer = $(sender).parents("[name=MultiSelectContainer]:first");
    var allChecked = controlContainer.find("input:checkbox:checked");
    if (allChecked.length > 0) {
        controlContainer.removeClass("MultiSelect_ChangedFieldCssId");
        controlContainer.addClass("MultiSelect_ChangedFieldCssId");
        allChecked.attr("checked", false);
        var unCheckedImage = controlContainer.find("#" + controlContainer.attr("ID") + "_ImgUnchecked").css("background-image");
        controlContainer.find(".MultiSelect__CheckBox").css("background-image", unCheckedImage);
        if (!doNotListSelectedInTextBox) {
            var inputItem = controlContainer.find("input:text");
            inputItem.val(emptyMessage);
            inputItem.attr("title", emptyMessage);
            inputItem.addClass("MultiSelect__EmptyTextBox");
        }
    }
    if (event != null) {
        event.stopPropagation();
    }
}
function HandleSelectAllEvent(sender, event, doNotListSelectedInTextBox) {
    var controlContainer = $(sender).parents("[name=MultiSelectContainer]:first");
    var allUnchecked = controlContainer.find("input:checkbox:not(:checked)");
    if (allUnchecked.length > 0) {
        controlContainer.removeClass("MultiSelect_ChangedFieldCssId");
        controlContainer.addClass("MultiSelect_ChangedFieldCssId");
        allUnchecked.attr("checked", true);
        var checkedImage = controlContainer.find("#" + controlContainer.attr("ID") + "_ImgChecked").css("background-image");
        controlContainer.find(".MultiSelect__CheckBox").css("background-image", checkedImage);
        if (!doNotListSelectedInTextBox) {
            var currentText = "";
            controlContainer.find(".MultiSelect__ItemText").each(function () {
                if (currentText.length > 0)
                    currentText += ", ";
                currentText += $(this).text();
            });
            var inputItem = controlContainer.find("input:text");
            inputItem.val(currentText);
            inputItem.attr("title", currentText);
            inputItem.removeClass("MultiSelect__EmptyTextBox");
        }
    }
    if (event != null) {
        event.stopPropagation();
    }
}